const initialState = {
  commentList: [] as CommentType[],
  originComment: {} as CommentType,

  replyData: {} as ReplyDataType,
};
interface ReplyDataType {
  total_count: number;
  end_id: string;
  last_id: string;
  results: CommentType[];
}

export type ActionType =
  | { type: 'comment/saveList'; payload: CommentType[] }
  | { type: 'comment/clearList' }
  | { type: 'comment/saveOriginComment'; payload: CommentType }
  | { type: 'comment/saveReplyList'; payload: ReplyDataType }
  | { type: 'comment/clearReplyList' };

export default function commentReducer(
  state = initialState,
  action: ActionType
): typeof initialState {
  switch (action.type) {
    case 'comment/saveList':
      return {
        ...state,
        commentList: [...action.payload],
      };

    case 'comment/clearList':
      return {
        ...state,
        commentList: [],
      };
    case 'comment/saveOriginComment':
      return {
        ...state,
        originComment: { ...action.payload },
      };
    case 'comment/saveReplyList':
      return { ...state, replyData: { ...action.payload } };

    case 'comment/clearReplyList':
      return {
        ...state,
        replyData: {} as ReplyDataType,
      };

    default:
      return state;
  }
}

export type CommentType = {
  com_id: string;
  content: string;
  reply_count: number;
  pubdate: string;
  is_followed: boolean;
  is_liking: boolean;
  like_count: number;
  aut_id: string;
  aut_name: string;
  aut_photo: string;
};
